define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/user_coupon/index' + location.search,
                    add_url: 'course/user_coupon/add',
                    edit_url: 'course/user_coupon/edit',
                    del_url: 'course/user_coupon/del',
                    multi_url: 'course/user_coupon/multi',
                    import_url: 'course/user_coupon/import',
                    table: 'user_coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'student_name', title: __('学员名称'),visible: false, operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User')},
                        {field: 'user.mobile', title: __('User.mobile')},
                        // {field: 'coupon.title', title: __('Coupon.title'), operate: 'LIKE'},
                        // {field: 'coupon_id', title: __('Coupon_id')},

                        {field: 'type', title: __('Type'), searchList: Config.typeList,formatter: Table.api.formatter.label},
                        {field: 'title', title: __('Title'), operate: 'LIKE' ,formatter: Controller.api.formatter.info},

                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: Config.statusList,formatter: Table.api.formatter.label},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'verify',
                                    text: __('核销'),
                                    title: __('核销'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    // icon: 'fa fa-list',
                                    confirm:"确定要核销当前优惠券吗？",
                                    url: 'course/user_coupon/verify',
                                    visible: function (row) {
                                        if( row.status == 1){
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                        //返回true时按钮显示,返回false隐藏
                                        return false;
                                    },
                                    refresh:true
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                info: function (value, row, index) {
                    var msg = "满" + row.money + "减" + row.reduction_money + "元";
                    return '<div >' +
                        '优惠券名称:' + row.coupon.title +'<br/>' + msg + "<br/>开始时间：" + row.start_time_text + "<br/>结束时间："+row.end_time_text +
                        '</div>';
                },

            },
        }
    };
    return Controller;
});
